﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class  tblUserDAL:ItblUser
    {
        HKEA_CMSEntities model;
        tblUser user;

        public tblUserDAL()
        {
            model = DataAccess.GetModelInstance();
            user = model.tblUsers.CreateObject();
        }

        public static tblUserDAL GetInstance()
        {
            return new tblUserDAL();
        }

       
        /// <summary>
        ///  获取一条记录
        /// </summary>
        /// <param name="ent">user 实体</param>
        /// <returns>user 实体</returns>
        public tblUser GetENT(tblUser ent)
        {
            var result = model.tblUsers.Where(a => a.UserID !="").Where(a=>a.Deleted==0);
            if (!string.IsNullOrEmpty(ent.User_Name))
                result = result.Where(a => a.User_Name == ent.User_Name);
            if (!string.IsNullOrEmpty(ent.User_Email))
                result = result.Where(a => a.User_Email == ent.User_Email);
            return result.FirstOrDefault();
        }

        public int InsertENT(tblUser ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblUsers.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblUser> GetListByENT(tblUser ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblUsers.Where(a => a.UserID !="");
            if (!string.IsNullOrEmpty(ent.UserID))
                result = result.Where(a => a.UserID == ent.UserID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblUser> GetListByENT(tblUser ent)
        {
            var result = model.tblUsers.Where(a => a.UserID != "");
            if (!string.IsNullOrEmpty(ent.UserID))
                result = result.Where(a => a.UserID == ent.UserID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblUser ent)
        {
            var result = model.tblUsers.Where(a => a.UserID != "");
            if (!string.IsNullOrEmpty(ent.UserID))
                result = result.Where(a => a.UserID == ent.UserID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblUser ent)
        {
            user = model.tblUsers.Where(a=>a.UserID==ent.UserID).FirstOrDefault();
            if (user != null)
            {
                user.LastModifyBy = ent.LastModifyBy;
                user.LastModifyDate = DateTime.Now;
                user.PositionUID = ent.PositionUID;
                user.User_Name = ent.User_Name;
                user.User_Password = ent.User_Password;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblUser ent)
        {
            var temp = model.tblUsers.Where(a => a.UserID !="");
            if (!string.IsNullOrEmpty(ent.UserID))
                temp = temp.Where(a=>a.UserID==ent.UserID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblUsers.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
